<script setup lang="ts">
import { addPatient, getPatientList, editPatient,delPatient } from "@/api/user";
import type { PatientList, Patient } from "@/types/user";
import { closeDialog, showFailToast, showSuccessToast, showConfirmDialog } from "vant";
import { onMounted, ref, computed } from "vue";
//导入校验身份证插件
import Validator from "id-validator";

//  测试号
// - 110101198307212600
// - 110101196107145504
// - 11010119890512132X
// - 110101196501023433
// - 110101197806108758
// - 110101198702171378
// - 110101198203195893
// - 如有雷同纯属巧合，可删除。

// 1. 查询家庭档案-患者列表
const list = ref<PatientList>([]);
const loadList = async () => {
  const res = await getPatientList();
  console.log("换着列表", res);
  list.value = res;
};
onMounted(() => {
  loadList();
});
const show = ref(false);
const showPopup = (item?: Patient) => {
  // patient.value={...defaultPathient}//每次弹出都加载默认值，不保存上次的数据
  if (item) {
    // 如果点的是编辑，解构出后台需要的数据
    const { id, gender, name, idCard, defaultFlag } = item;
    patient.value = { id, gender, name, idCard, defaultFlag };
  } else {
    patient.value = { ...defaultPathient };
  }
  show.value = true;
};
const closeDialogs = () => {
  show.value = false;
};
const options = [
  {
    lable: "男",
    value: 1,
  },
  {
    lable: "女",
    value: 0,
  },
];
const gender = ref(1);
const defaultPathient: Patient = {
  name: "",
  idCard: "",
  gender: 1,
  defaultFlag: 1,
};
const patient = ref<Patient>({
  name: "",
  idCard: "",
  gender: 1,
  defaultFlag: 1,
});
// 默认值需要转换
const defaultFlag = computed({
  get() {
    return patient.value.defaultFlag === 1 ? true : false;
  },
  set(value) {
    patient.value.defaultFlag = value ? 1 : 0;
  },
});
//创建校验身份证校验
const cardValid = new Validator();
const submit = async () => {
  if (!patient.value.name) return showFailToast("请输入真实姓名");
  if (!patient.value.idCard) return showFailToast("请输入身份证号");

  if (!cardValid.isValid(patient.value.idCard)) {
    return showFailToast("身份证格式错误");
  }
  const { sex } = cardValid.getInfo(patient.value.idCard);
  if (patient.value.gender !== sex) return showFailToast("性别和身份证不符");
  try {
    //  await addPatient(patient.value)
    patient.value.id
      ? await editPatient(patient.value)
      : await addPatient(patient.value);
    closeDialog();
    loadList();
    //  showSuccessToast('添加成功')
    showSuccessToast(patient.value.id ? "编辑成功" : "添加成功");
  } catch (error) {}
};

const remove = async () => {
  if (patient.value.id) {
    await showConfirmDialog({
      title: '温馨提示',
      message: `您确认要删除 ${patient.value.name} 患者信息吗 ？`
    })
    await delPatient(patient.value.id)
    show.value = false
    loadList()
    showSuccessToast('删除成功')
  }
}
</script>

<template>
  <div class="patient-page">
    <cp-nav-bar title="家庭档案" />
    <!-- 头部选择提示 -->
    <div class="patient-change" v-if="false">
      <h3>请选择患者信息</h3>
      <p>以便医生给出更准确的治疗，信息仅医生可见</p>
    </div>
    <div class="patient-list">
      <div class="patient-item" v-for="(item, index) in list" :key="index">
        <div class="info">
          <span class="name">{{ item.name }}</span>
          <span class="id">{{
            item.idCard.replace(/^(.{6})(?:\d+)(.{4})$/, "\$1******\$2")
          }}</span>
          <span>{{ item.genderValue }}</span>
          <span>{{ item.age }}岁</span>
        </div>
        <div @click="showPopup(item)" class="icon">
          <cp-icon name="user-edit" />
        </div>
        <div class="tag">默认</div>
      </div>
      <!-- <div class="patient-item">
        <div class="info">
          <span class="name">李富贵</span>
          <span class="id">321***********6164</span>
          <span>男</span>
          <span>32岁</span>
        </div>
        <div class="icon"><cp-icon name="user-edit" /></div>
      </div> -->
      <div class="patient-add" @click="showPopup()">
        <cp-icon name="user-add" />
        <p>添加患者</p>
      </div>
      <div class="patient-tip">最多可添加 6 人</div>
    </div>
    <!-- 患者选择下一步 -->
    <div class="patient-next" v-if="false">
      <van-button type="primary" round block>下一步</van-button>
    </div>
    <!-- 侧边栏 -->
    <van-popup v-model:show="show" position="right">
      <cp-nav-bar
        :title="patient.id ? '编辑患者' : '添加患者'"
        right-text="保存"
        :back="closeDialogs"
        @click-right="submit"
      ></cp-nav-bar>
      <!-- 表单 -->
      <div>
        <van-form autocomplete="off">
          <van-field
            label="真实姓名"
            placeholder="请输入真实姓名"
            v-model="patient.name"
          />
          <van-field
            label="身份证号"
            placeholder="请输入身份证号"
            v-model="patient.idCard"
          />
          <van-field label="性别">
            <!-- 单选按钮组件 -->
            <template #input>
              <cp-radio-btn
                :options="options"
                v-model:model-value="patient.gender"
              ></cp-radio-btn>
            </template>
          </van-field>
          <van-field label="默认就诊人">
            <template #input>
              <van-checkbox round v-model="defaultFlag" />
            </template>
          </van-field>

          <van-action-bar>
            <van-action-bar-button
              type="danger"
              text="删除"
               @click="remove"
            ></van-action-bar-button>
          </van-action-bar>
        </van-form>
      </div>
    </van-popup>
    <!-- <cp-radio-btn
      :options="options"
      v-model:model-value="gender"
    ></cp-radio-btn> -->
  </div>
</template>

<style lang="scss" scoped>
.patient-page {
  ::v-deep() {
    .van-popup {
      padding: 46px 0 80px;
      width: 90%;
      height: 100%;
      box-sizing: border-box;
    }
  }
}
.patient-page {
  padding: 46px 0 80px;
}
.patient-change {
  padding: 15px;
  > h3 {
    font-weight: normal;
    margin-bottom: 5px;
  }
  > p {
    color: var(--cp-text3);
  }
}
.patient-next {
  padding: 15px;
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 80px;
  box-sizing: border-box;
}
.patient-list {
  padding: 15px;
}
.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;
  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;
      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }
      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }
  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }
  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    .icon {
      color: var(--cp-primary);
    }
  }
}
.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
</style>
